﻿using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;

namespace Hotkey
{
    public class Win32
    {
        private static Stopwatch stopwatch;
        private const int WH_KEYBOARD_LL = 13;
        private const int WM_KEYDOWN = 0x0100;
        private static LowLevelKeyboardProc _proc = HookCallback;
        public static IntPtr _hookID = IntPtr.Zero;
        private static double MouseDoubleClickTime;

        public delegate void HookEventHandler(object sender, HookEventArgs e);
        public static event HookEventHandler HookInvoked;

        public static IntPtr SetHook()
        {
            MouseDoubleClickTime = GetDoubleClickTime() / 1000.0;
            stopwatch = new Stopwatch();
            stopwatch.Start();
            using (Process curProcess = Process.GetCurrentProcess())
            using (ProcessModule curModule = curProcess.MainModule)
            {
                return SetWindowsHookEx(WH_KEYBOARD_LL, _proc,
                    GetModuleHandle(curModule.ModuleName), 0);
            }
        }

        private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

        private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0 
                && wParam == (IntPtr)WM_KEYDOWN
                && Marshal.ReadInt32(lParam) == 'C')
            {
                double elapsed = stopwatch.ElapsedMilliseconds / 1000.0;
                stopwatch.Restart();
                if (elapsed < MouseDoubleClickTime)
                {
                    // This would be where something happens.
                    var text = Clipboard.GetText();
                    if (text != string.Empty)
                    {
                        HookInvoked(text, new HookEventArgs(text));
                        Debug.WriteLine(text);
                    }
                }
            }
            return CallNextHookEx(_hookID, nCode, wParam, lParam);
        }

        public static void StopTimer()
        {
            stopwatch.Stop();
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern int GetDoubleClickTime();
    }

    public class HookEventArgs : EventArgs
    {
        private string HookText { get; set; }

        public HookEventArgs(string text)
        {
            HookText = text;
        }
    }
}
